var send = $api.byId('send');
var times = 60; 
var timer = null;
var mobile = '';
function _send(){

	var m = $api.byId('mobile');
	mobile = $api.val(m);
	if(checkmobile(mobile)){
		$api.attr(send,'disabled','disabled')
		api.ajax({
			url:'http://wap.jqmao.cn/getRetrievePin',
			method:'post',
			data:{
				values:{mobile: mobile}
			}
		},function(ret,err){
			if(ret){
				
				if(ret.code==0){
					timer = setInterval(djs,1000);
				}else if(ret.code==2){
					toast('该用户不存在');
					$api.removeAttr(send,'disabled');
				}else{
					toast('验证码暂时无法发送');
					$api.removeAttr(send,'disabled');
				}
				
			}else{
				//alert(JSON.stringify(err));
			}
		})
	}else{
		toast('请输入正确的手机号');
		$api.removeAttr(send,'disabled')
	}
	
	
}
 var djs = function(){
 		$api.html(send,times+"秒后重试");
   
       /* send.attr('disabled','disabled');*/
       /*$api.attr(send,'disabled','disabled')*/
        
        times--;
        if(times <= 0){
        	$api.html(send,"发送验证码");
            $api.removeAttr(send,'disabled')
            /*send.removeAttr('disabled');*/
            times = 60;
            clearInterval(timer);
        }
    }

  function checkmobile(mobile){
  		var t = /^1\d{10}$/;
  		var c =t.test(mobile);
  		return c;
  }
  function checkpwd(pwd){
  	var p1 = /^([\x00-\xff]){6,20}$/;
    var p2 = /^\S{6,20}$/;
    var p1 = p1.test(pwd);
    var p2 = p2.test(pwd);
    if(p1&&p2){
    	return true;
    }else{
    	return false;
    }

  }
  function register(){
  	save();
  }
  //提交数据 保存数据
  function save(){
  	var m = $api.byId('mobile');
	var _mobile = $api.val(m);
	if(checkmobile(_mobile)==false){
		toast('请输入正确的手机号');
		return;
	}

	var p = $api.byId('pin');
	var pin = $api.val(p);
	var pw = $api.byId('pwd');
	var pwd = $api.val(pw);
	if(checkpwd(pwd)==false){
		toast('请输入6-20位密码');
		return;
	}
	if(_mobile&&pin&&pwd){
		/*if(_mobile==mobile){*/
			api.ajax({
			url:'http://wap.jqmao.cn/saveMember',
			method:'post',
			data:{
				values:{mobile: _mobile,pin: pin,pwd: pwd}
			}
		},function(ret,err){
			if(ret){
				
				if(ret.code==0){
					if(ret.member){
						$api.setStorage('user',ret.member);

                            var user = $api.getStorage('user');
                            if(user){
							                              
							var username = $api.getStorage('user').username;
								
								var	uid = $api.getStorage('user').uid;
								var push = api.require('push');
								push.bind({
								    userName:''+username,
								    userId:''+uid
								},function(ret,err){
								    if(ret.status){
								        api.alert({msg:'绑定成功'});
								    }else{
								        //api.alert({msg:err.msg});
								    }
								});


                                api.execScript({
                                    name:'root',
                                    frameName: 'user',
                                    script: 'userReload()'
                                });
                                //api.closeWin();
                         
                                        home();
					}
				}
					//完成注册

				}else if(ret.code==2){
					toast('该用户已存在');
				}else if(ret.code==3){
					toast('该用户未发送验证信息');
				}else{
					toast('验证码错误');
				}
				
			}else{
				//alert(JSON.stringify(err));
			}
		})
		/*}else{
			toast('该手机号未验证');
		}*/
	}else{
		toast('请完成内容');
	}
  }
apiready = function(){
    headerstatus()
}
